#pragma once
#include <iostream>
#include <string>
#include <sstream>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>

/*-------------- 网络地址跟主机地址之间进行转换 -----------------*/

class InetAddr
{
public:
    InetAddr(struct sockaddr_in& sock):_sock(sock)
    {
        _port = ntohs(sock.sin_port);   //从网络中拿到字节序
        _ip = inet_ntoa(sock.sin_addr);            // 4字节网络风格的IP -> 点分十进制的字符串风格的IP
    }
    
    std::string Ip()
    {
        return _ip;
    }
    int Port()
    {
        return _port;
    }

    bool operator==(const InetAddr& peer)
    {
        return peer._ip == _ip && peer._port == _port;
    }

    std::string StringAddr()
    {
        return _ip + ":" + std::to_string(_port);
    }


private:
    struct sockaddr_in _sock;
    std::string _ip;
    int _port;
};
